package bairui.international.service.impl;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sh_lianjing.util.Util_UUID;

import bairui.international.common.config.Config_Path;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffRoyaltyRecord;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_ProductOrderItem;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.domain.jpa.id.ID_ProductOrderItem;
import bairui.international.model.type.Type_ProductOrderItem;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductOrderItem;
import bairui.international.service.Service_ProductPropertyItem;

/**
 * 
 * 【(产品订单项基础)TB_ProductOrderItem】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_ProductOrderItem 模型
 * @see Service_ProductOrderItem 服务接口
 * @see BService_ProductOrderItemImpl 服务实现基础
 *
 */
@Service
public class Service_ProductOrderItemImpl extends BService_ProductOrderItemImpl implements Service_ProductOrderItem {

	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	@Autowired private Service_Product service_Product;
	@Autowired protected Config_Path pathConfig;
	
	private TB_ProductOrderItem createInstance(TB_Product _product, TB_ProductOrder productOrder, Type_ProductOrderItem type, long amount){
		TB_Product product = service_Product.load(_product);
		double productPerPrice = product.getPrice();
		double productTotalPrice = BigDecimal.valueOf(productPerPrice)
				.multiply(BigDecimal.valueOf(amount))
				.doubleValue();
		double productScore = product.getScore();
		double productTotalScore = BigDecimal.valueOf(productScore)
				.multiply(BigDecimal.valueOf(amount))
				.doubleValue();
		/* 拷贝产品图片 */
		String imgUrl = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + File.separator + Util_UUID.getUUID();
		File source = new File(pathConfig.getPosition_Image_Product() + product.getImgUrl());
		if(source.exists()){
			try {
				FileUtils.copyFile(
						source, 
						new File(pathConfig.getPosition_Image_ProductOrderItem() + imgUrl)
					);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		/* 创建 */
		return dao.save(new TB_ProductOrderItem(
				new TB_ProductOrderItem.MustProperties() {
					@Override public Type_ProductOrderItem getType() {
						return type;
					}
					@Override public Double getProductTotalPrice() {
						return productTotalPrice;
					}
					@Override public Double getProductScore() {
						return productScore;
					}
					@Override public Double getProductPerPrice() {
						return productPerPrice;
					}
					@Override public ID_ProductOrderItem getId() {
						return new ID_ProductOrderItem(product, productOrder);
					}
					@Override public Long getAmount() {
						return amount;
					}
					@Override public Double getProductTotalScore() {
						return productTotalScore;
					}
				},
				new TB_ProductOrderItem.OptionalSingleProperties() {
					@Override public String getProductUnit() {
						return gainUnit(product);
					}
					@Override public String getProductNumber() {
						return product.getNumber();
					}
					@Override public String getProductName() {
						return product.getName();
					}
					@Override public String getProductImgUrl() {
						return imgUrl;
					}
					@Override public String getProductComment() {
						return product.getComment();
					}
					@Override public TB_BranchCompanyStaffRoyaltyRecord getBranchCompanyStaffRoyaltyRecord() {
						return null;
					}
				}
			));
	}
	
	@Override
	public TB_ProductOrderItem createForGrade(TB_Product product, TB_ProductOrder productOrder, long amount){
		return createInstance(product, productOrder, Type_ProductOrderItem.Grade, amount); 
	}
	
	@Override
	public TB_ProductOrderItem createForProduct(TB_Product product, TB_ProductOrder productOrder, long amount){
		return createInstance(product, productOrder, Type_ProductOrderItem.Product, amount); 
	}
	
	@Override
	public TB_ProductOrderItem createForPack(TB_Product product, TB_ProductOrder productOrder, long amount){
		return createInstance(product, productOrder, Type_ProductOrderItem.Pack, amount); 
	}
	
	private String gainUnit(TB_Product product){
		// 处理单位
		TB_ProductPropertyItem p = service_ProductPropertyItem.findOne(it->
			it.productProperty.name.eq("单位").and(it.products.any().eq(product))
		);
		String unit = null;
		if(null != p){
			unit = p.getName();
		}
		return unit;
	}
	
}
